<template>
  <div class="get">
    <div class="header" unselectable="on">
      <div class="head_total">
        <span class="name">收件箱</span>
        <span class="num">（共{{this.total}}封）</span>
        <el-button type="info" size="mini" plain style="margin-left:20px;" @click="refresh()"><i class="el-icon-refresh"></i> 刷新</el-button>
      </div>
      <div>
        <div class="page_num">{{this.pageNum}}/{{this.pages}}</div>
        <div class="page_btns">
          <div :class="['lf_btn', pageNum>1?'':'nui-btn-disabled']" @click="pageNum>1?changeNum('cut'):''">
            <span class="nui-btn-icon">
              <b class="nui-ico"></b>
            </span>
            <span class="nui-btn-text">上一页</span>
          </div>
          <div :class="['rt_btn', pageNum==pages?'nui-btn-disabled':'']" @click="pageNum==pages?'':changeNum('add')">
            <span class="nui-btn-icon">
              <b class="nui-ico"></b>
            </span>
            <span class="nui-btn-text">下一页</span>
          </div>
        </div>
      </div>
    </div>
    <div class="list">
      <el-table
        class="table"
        v-loading="loading"
        :data="list"
        tooltip-effect="dark"
        @row-click="goDetail($event)">
        <el-table-column
          label="@"
          width="35">
          <template slot-scope="scope">
            <i :class="[scope.row.isRead=='2'?'read':'noRead', 'el-icon-message']" :title="scope.row.isRead=='2'?'已读':'未读'"></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="nick"
          label="发件人"
          width="180">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="title"
          label="主题">
        </el-table-column>
        <el-table-column
          prop="created"
          fixed="right"
          width="170"
          label="时间">
        </el-table-column>
        <el-table-column
          fixed="right"
          width="100"
          label="操作">
          <template slot-scope="scope">
            <el-button type="info" size="mini" plain @click="toDelete(scope.row, $event)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { mailList, mailEdi } from '../../apis/api.js'
import { storage } from '../../utils/storage.js'
export default {
  data() {
    return {
      list: [],
      loading: false,
      total: 0,
      pages: 1,
      pageSize: 3,
      pageNum: 1,
    };
  },
  methods: {
    getList() {
      this.loading = true;
      mailList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        show: 2,
        type: 1,
        userId: storage.getSession('id')
      }).then(res => {
        console.log(res)
        this.list = res.list;
        this.total = res.total;
        this.pageNum = res.pageNum;
        this.pages = 
        (res.total < res.pageSize) ? 1 : ((res.total % res.pageSize) ? (parseInt(res.total / res.pageSize) + 1) : (res.total / res.pageSize));
        this.loading = false;
      })
    },
    refresh() {
      this.getList();
    },
    goDetail(item) {
      console.log(item)
      this.$router.push(`./det/${item.id}`);
    },
    toDelete(item, e) {
      e.stopPropagation();
      mailEdi({
        isRead: item.isRead,
        mailId: item.id,
        type: '3'
      }).then(res => {
        this.$message.success('邮件删除成功')
        console.log(res)
      })
    },
    changeNum(type) {
      console.log(type)
      if(type == 'add') {
        this.pageNum++;
      } else if (type == 'cut') {
        this.pageNum--;
      }
      this.getList()
    }
    // ediIsRead(item, e) {
    //   e.stopPropagation();
    //   console.log(item)
    //   var state = item.isRead == '1' ? '2' : '1';
    //   mailEdi({
    //     isRead: state,
    //     mailId: item.id,
    //     type: '1'
    //   }).then(res => {
    //     console.log(res)
    //   })
    // }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style scoped>
.get {
  font-size: 12px;
}
.listBtn {
  cursor: pointer;
  padding-right: 10px;
}
.listBtn:hover {
  color: #951b1b;
}

i.noRead {
  color: #710505;
}
.table {
  width: 100%;
}
</style>